﻿using System;
using System.Drawing;
using System.Windows.Forms;
using QuanLyThuPhiTaiLieu.Biz.QuanTriHeThong;
using QuanLyThuPhiTaiLieu.Common;

namespace QuanLyThuPhiTaiLieu.Forms.QuanTriHeThong
{
    public partial class ChangePassword : Form
    {
        public ChangePassword()
        {
            InitializeComponent();
        }

        private void FormLoad(object sender, EventArgs e)
        {
            var parent = (MDI)MdiParent;

            if (parent.CurrentUser != null)
            {
                textAccount.Text = parent.CurrentUser.Account;
            }
            else if (parent.CurrentGuest != null)
            {
                textAccount.Text = parent.CurrentGuest.Account;
            }
            textPassword.Focus();
        }
        
        private bool Validation()
        {
            bool hasError = true;
            bool isFocused = false;
            labelMessage.Text = string.Empty;
            errorProvider.Clear();

            if (string.IsNullOrEmpty(textPassword.Text))
            {
                errorProvider.SetError(textPassword, Constants.Messages.RequireMessage);
                hasError = false;

                if (!isFocused)
                {
                    textPassword.Focus();
                }
            }
            else
            {
                var parent = (MDI)MdiParent;

                if (parent.CurrentUser != null)
                {
                    var biz = new NguoiSuDungBiz();
                    var item = biz.Login(parent.CurrentUser.Account, textPassword.Text);
                    if (item == null)
                    {
                        errorProvider.SetError(textPassword, Constants.Messages.PasswordIsvalidMessage);
                        hasError = false;

                        if (!isFocused)
                        {
                            textPassword.Focus();
                        }
                    }
                }
                else if (parent.CurrentGuest != null)
                {
                    var biz = new GuestBiz();
                    var item = biz.Login(parent.CurrentGuest.Account, textPassword.Text);
                    if (item == null)
                    {
                        errorProvider.SetError(textPassword, Constants.Messages.PasswordIsvalidMessage);
                        hasError = false;

                        if (!isFocused)
                        {
                            textPassword.Focus();
                        }
                    }
                }
            }

            if (string.IsNullOrEmpty(textNewPassword.Text))
            {
                errorProvider.SetError(textNewPassword, Constants.Messages.RequireMessage);
                hasError = false;

                if (!isFocused)
                {
                    textNewPassword.Focus();
                }
            }

            if (string.IsNullOrEmpty(textConfirmNewPassword.Text))
            {
                errorProvider.SetError(textConfirmNewPassword, Constants.Messages.RequireMessage);
                hasError = false;

                if (!isFocused)
                {
                    textConfirmNewPassword.Focus();
                }
            }

            if (!string.IsNullOrEmpty(textConfirmNewPassword.Text) && !string.IsNullOrEmpty(textNewPassword.Text) && 
                !string.Equals(textConfirmNewPassword.Text, textNewPassword.Text, StringComparison.InvariantCultureIgnoreCase))
            {
                errorProvider.SetError(textConfirmNewPassword, Constants.Messages.ConfirmNewPasswordMessage);
                hasError = false;

                if (!isFocused)
                {
                    textConfirmNewPassword.Focus();
                }
            }

            return hasError;
        }

        protected void ChangeSize(object sender, EventArgs e)
        {
            int width = Size.Width / 2 - groupBox1.Size.Width / 2;
            int height = Size.Height / 2 - groupBox1.Size.Height / 2;
            groupBox1.Location = new Point(width, height);
        }

        private void Cancel(object sender, EventArgs e)
        {
            var parrent = (MDI)MdiParent;
            parrent.ShowMenu(true);
            Utilities.ShowMdi(this);
        }

        private void Change(object sender, EventArgs e)
        {
            if (Validation())
            {
                var parent = (MDI)MdiParent;

                if (parent.CurrentUser != null)
                {
                    var biz = new NguoiSuDungBiz();
                    var item = biz.LoadItem(parent.CurrentUser.Id);
                    item.Password = Utilities.EncodePassword(textNewPassword.Text);
                    biz.UpdateItem(item);
                }
                else if (parent.CurrentGuest != null)
                {
                    var biz = new GuestBiz();
                    var item = biz.LoadItem(parent.CurrentUser.Id);
                    item.Password = Utilities.EncodePassword(textNewPassword.Text);
                    biz.UpdateItem(item);
                }            

                parent.ShowMenu(true);
                Utilities.ShowMdi(this);
            }            
        }
    }
}
